﻿/* Ree7 Tile Toolkit for Windows Phone
 * Copyright (C) 2011-2014 Pierre BELIN <pierre@ree7.fr>, and project contributors.
 *
 * Distributed under the Microsoft Public License (Ms-PL).
 * See accompanying file License.txt */

using Microsoft.Phone.Shell;
using System;

namespace Ree7.TileToolkit
{
    /// <summary>
    /// Provides backward compatibility for Windows Phone 7.x
    /// </summary>
    public static class ShellTileEx
    {
        private static readonly Version WPVER_7_8 = new Version(7, 10, 8858);

        public static void Create(Uri uri, ShellTileData tiledata, bool usewide)
        {
            Type shellTileType = Type.GetType("Microsoft.Phone.Shell.ShellTile, Microsoft.Phone, Version=8.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e");
            var createmethod = shellTileType.GetMethod("Create", new Type[] { typeof(Uri), typeof(ShellTileData), typeof(bool) });
            createmethod.Invoke(null, new object[] { uri, tiledata, usewide });
        }

        /// <summary>
        /// On Windows Phone 7.8 and newer, returns a FlipTileData with all the parameters set
        /// On Windows Phone 7.5 fallback using StandardTileData with only a limited set of parameters
        /// </summary>
        public static StandardTileData CreateFlipTileData(
            string title,
            int? count,
            string backTitle,
            string backContent,
            Uri smallBackgroundImage,
            Uri backgroundImage,
            Uri backBackgroundImage,
            Uri wideBackgroundImage,
            Uri wideBackBackgroundImage,
            string wideBackContent)
        {
            Type flipTileDataType = Type.GetType("Microsoft.Phone.Shell.FlipTileData, Microsoft.Phone, Version=8.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e");
            if (flipTileDataType != null)
            {
                // Windows Phone 7.8, 8.0, ...
                var mynewtile = flipTileDataType.GetConstructor(new Type[] { }).Invoke(null);
                SetProperty(mynewtile, "Title", title);
                SetProperty(mynewtile, "Count", count);
                SetProperty(mynewtile, "BackTitle", backTitle);
                SetProperty(mynewtile, "BackContent", backContent);
                SetProperty(mynewtile, "SmallBackgroundImage", smallBackgroundImage);
                SetProperty(mynewtile, "BackgroundImage", backgroundImage);
                SetProperty(mynewtile, "BackBackgroundImage", backBackgroundImage);
                SetProperty(mynewtile, "WideBackgroundImage", wideBackgroundImage);
                SetProperty(mynewtile, "WideBackBackgroundImage", wideBackBackgroundImage);
                SetProperty(mynewtile, "WideBackContent", wideBackContent);

                return (StandardTileData)mynewtile;
            }
            else
            {
                // Windows Phone 7.5
                return new StandardTileData()
                {
                    BackBackgroundImage = backBackgroundImage,
                    BackContent = backContent,
                    BackgroundImage = backgroundImage,
                    BackTitle = backTitle,
                    Count = count,
                    Title = title
                };
            }
        }

        internal static void SetProperty(object instance, string name, object value)
        {
            var setMethod = instance.GetType().GetProperty(name).GetSetMethod();
            setMethod.Invoke(instance, new object[] { value });
        }
    }
}
